#include "Common.fxh"

Texture2D Specular;

struct VS_INPUT
{
    float4 Pos				: POSITION;         //position
    float3 Normal			: NORMAL;
    float2 Tex				: TEXCOORD;
    float3 Binormal			: BINORMAL;
    float3 Tangent			: TANGENT;
    row_major float4x4 World	: mTransform;
};

struct PS_INPUT
{
    float4 Pos : SV_POSITION;
    float2 Tex : TEXCOORD0;
    float3x3 tangentToWorld : TEXCOORD2;
};

struct PS_OUTPUT
{
	float4 Diffuse : SV_TARGET0;
	float4 Normal : SV_TARGET1;
};


//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
PS_INPUT VS( VS_INPUT input )
{
    PS_INPUT output = (PS_INPUT)0;
    
    float4 worldPosition = mul(input.Pos, input.World);
    output.Pos = mul(worldPosition, ViewProj);
    output.Tex = input.Tex;
    output.tangentToWorld[0] = mul(input.Tangent, (float3x3)input.World);
	output.tangentToWorld[1] = mul(input.Binormal, (float3x3)input.World);
	output.tangentToWorld[2] = mul(input.Normal, (float3x3)input.World);
    
    return output;
}

PS_OUTPUT PS( PS_INPUT input)
{
	PS_OUTPUT output = (PS_OUTPUT)0;

    //calculate lighting assuming light color is <1,1,1,1>
    float4 normal = Normal.Sample(samLinear, input.Tex);
    float4 diffuse = Diffuse.Sample(samLinear, input.Tex); 
    float4 specular = Specular.Sample(samLinear, input.Tex);
    
    float3 normalFromMap = 2.0 * normal.rgb - 1.0;
	normalFromMap = normalize(mul(normalFromMap, input.tangentToWorld));
    
    output.Diffuse.rgb = diffuse.rgb;
	output.Diffuse.a = specular.r;
    output.Normal.rgb = 0.5 * (normalFromMap + 1.0);
    output.Normal.a = specular.a;
    return output;
}

technique10 Render
{
    pass P0
    {
        SetVertexShader( CompileShader( vs_4_0, VS() ) );
        SetGeometryShader( NULL );
        SetPixelShader( CompileShader( ps_4_0, PS() ) );
    }
}

